
<!DOCTYPE HTML>
<html lang="zh-hans" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>rabbitMQ12案例 · RabbitMQ</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        <meta name="author" content="<yangxin19970404@163.com>">
        
        
    
    <link rel="stylesheet" href="gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="rabbitMQ13work模式.html" />
    
    
    <link rel="prev" href="rabbitMQ11核心组成部分.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="输入并搜索" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="./">
            
                <a href="./">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="rabbitMQ01什么是中间件.html">
            
                <a href="rabbitMQ01什么是中间件.html">
            
                    
                    rabbitMQ01什么是中间件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="rabbitMQ02基于消息中间件的分布式系统.html">
            
                <a href="rabbitMQ02基于消息中间件的分布式系统.html">
            
                    
                    rabbitMQ02基于消息中间件的分布式系统
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="rabbitMQ03消息队列协议.html">
            
                <a href="rabbitMQ03消息队列协议.html">
            
                    
                    rabbitMQ03消息队列协议
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="rabbitMQ04消息的持久化.html">
            
                <a href="rabbitMQ04消息的持久化.html">
            
                    
                    rabbitMQ04消息的持久化
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="rabbitMQ05消息的分发策略.html">
            
                <a href="rabbitMQ05消息的分发策略.html">
            
                    
                    rabbitMQ05消息的分发策略
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="rabbitMQ06高可用和高可靠.html">
            
                <a href="rabbitMQ06高可用和高可靠.html">
            
                    
                    rabbitMQ06高可用和高可靠
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="rabbitMQ07RabbitMQ入门及安装.html">
            
                <a href="rabbitMQ07RabbitMQ入门及安装.html">
            
                    
                    rabbitMQ07RabbitMQ入门及安装
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="rabbitMQ08Web管理界面及授权操作.html">
            
                <a href="rabbitMQ08Web管理界面及授权操作.html">
            
                    
                    rabbitMQ08Web管理界面及授权操作
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10" data-path="rabbitMQ09docker安装rabbitmq.html">
            
                <a href="rabbitMQ09docker安装rabbitmq.html">
            
                    
                    rabbitMQ09docker安装rabbitmq
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11" data-path="rabbitMQ10角色分类.html">
            
                <a href="rabbitMQ10角色分类.html">
            
                    
                    rabbitMQ10角色分类
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.12" data-path="rabbitMQ11核心组成部分.html">
            
                <a href="rabbitMQ11核心组成部分.html">
            
                    
                    rabbitMQ11核心组成部分
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.13" data-path="rabbitMQ12案例.html">
            
                <a href="rabbitMQ12案例.html">
            
                    
                    rabbitMQ12案例
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.14" data-path="rabbitMQ13work模式.html">
            
                <a href="rabbitMQ13work模式.html">
            
                    
                    rabbitMQ13work模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.15" data-path="rabbitMQ14springboot整合mq.html">
            
                <a href="rabbitMQ14springboot整合mq.html">
            
                    
                    rabbitMQ14springboot整合mq
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            本书使用 GitBook 发布
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="." >rabbitMQ12案例</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h3 id="&#x6848;&#x4F8B;">&#x6848;&#x4F8B;</h3>
<blockquote>
<p>producer</p>
</blockquote>
<pre><code class="lang-java"><span class="hljs-keyword">package</span> com.yang.rabbitmq.simple;

<span class="hljs-keyword">import</span> com.rabbitmq.client.Channel;
<span class="hljs-keyword">import</span> com.rabbitmq.client.Connection;
<span class="hljs-keyword">import</span> com.rabbitmq.client.ConnectionFactory;


<span class="hljs-comment">/**
 * ClassName: Producer
 * Description:
 * Date: 2021/4/7 20:55
 *
 * <span class="hljs-doctag">@author</span> YangXin
 */</span>
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Producer</span> </span>{

    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span> </span>{
        <span class="hljs-comment">//&#x6240;&#x6709;&#x7684;&#x4E2D;&#x95F4;&#x4EF6;&#x6280;&#x672F;&#x90FD;&#x662F;&#x57FA;&#x4E8E;tcp/ip&#x534F;&#x8BAE;&#x57FA;&#x7840;&#x4E0A;&#x6784;&#x5EFA;&#x65B0;&#x578B;&#x7684;&#x534F;&#x8BAE;&#x89C4;&#x8303;,&#x53EA;&#x4E0D;&#x8FC7;rabbitmq&#x9075;&#x5FAA;&#x7684;&#x662F;amqp&#x534F;&#x8BAE;</span>
        <span class="hljs-comment">//1. &#x521B;&#x5EFA;&#x8FDE;&#x63A5;&#x5DE5;&#x7A0B;</span>
        ConnectionFactory connectionFactory = <span class="hljs-keyword">new</span> ConnectionFactory();
        connectionFactory.setHost(<span class="hljs-string">&quot;39.105.46.187&quot;</span>);
        connectionFactory.setPort(<span class="hljs-number">5672</span>);
        connectionFactory.setUsername(<span class="hljs-string">&quot;admin&quot;</span>);
        connectionFactory.setPassword(<span class="hljs-string">&quot;admin&quot;</span>);
        connectionFactory.setVirtualHost(<span class="hljs-string">&quot;/&quot;</span>);

        Connection connection = <span class="hljs-keyword">null</span>;
        Channel channel = <span class="hljs-keyword">null</span>;
        <span class="hljs-keyword">try</span> {
            <span class="hljs-comment">//2. &#x521B;&#x5EFA;&#x8FDE;&#x63A5;Connection RabbitMQ&#x662F;&#x57FA;&#x4E8E;channel&#x5904;&#x7406;&#x800C;&#x4E0D;&#x662F;&#x57FA;&#x4E8E;&#x8FDE;&#x63A5;,&#x56E0;&#x4E3A;&#x8FDE;&#x63A5;&#x4F1A;&#x8FDB;&#x884C;&#x63E1;&#x624B;&#x8017;&#x8D39;&#x65F6;&#x95F4;&#x5E76;&#x4E14;&#x5F00;&#x95ED;&#x6781;&#x5EA6;&#x6D88;&#x8017;&#x8D44;&#x6E90;</span>
            connection = connectionFactory.newConnection(<span class="hljs-string">&quot;&#x751F;&#x4EA7;&#x8005;&quot;</span>);
            <span class="hljs-comment">//3. &#x901A;&#x8FC7;&#x8FDE;&#x63A5;&#x83B7;&#x53D6;&#x901A;&#x9053;Channel</span>
            channel = connection.createChannel();
            <span class="hljs-comment">//4. &#x901A;&#x8FC7;&#x521B;&#x5EFA;&#x4EA4;&#x6362;&#x673A;,&#x58F0;&#x660E;&#x961F;&#x5217;,&#x7ED1;&#x5B9A;&#x5173;&#x7CFB;,&#x8DEF;&#x7531;key,&#x53D1;&#x9001;&#x6D88;&#x606F;&#x548C;&#x63A5;&#x6536;&#x6D88;&#x606F;</span>
            String queueName = <span class="hljs-string">&quot;queue1&quot;</span>;

            <span class="hljs-comment">/*
             * @params1 &#x961F;&#x5217;&#x540D;&#x79F0;
             * @params2 &#x662F;&#x5426;&#x8981;&#x6301;&#x4E45;&#x5316;durable = false &#x6240;&#x8C13;&#x6301;&#x4E45;&#x5316;&#x6D88;&#x606F;&#x662F;&#x5426;&#x5B58;&#x76D8;,&#x5982;&#x679C;false &#x975E;&#x6301;&#x4E45;&#x5316; true&#x662F;&#x6301;&#x4E45;&#x5316;? &#x975E;&#x6301;&#x4E45;&#x5316;&#x4F1A;&#x5B58;&#x76D8;&#x4E48;?
             * @params3 &#x6392;&#x4ED6;&#x6027;,&#x662F;&#x5426;&#x662F;&#x72EC;&#x5360;&#x72EC;&#x7ACB;
             * @params4 &#x662F;&#x5426;&#x81EA;&#x52A8;&#x5220;&#x9664;,&#x968F;&#x7740;&#x6700;&#x540E;&#x4E00;&#x4E2A;&#x6D88;&#x8D39;&#x8005;&#x6D88;&#x606F;&#x5B8C;&#x6BD5;&#x6D88;&#x606F;&#x4E4B;&#x540E;&#x662F;&#x5426;&#x628A;&#x961F;&#x5217;&#x81EA;&#x52A8;&#x5220;&#x9664;
             * @params5 &#x643A;&#x5E26;&#x9644;&#x5C5E;&#x53C2;&#x6570;
             */</span>
            channel.queueDeclare(queueName, <span class="hljs-keyword">false</span>, <span class="hljs-keyword">false</span>, <span class="hljs-keyword">false</span>, <span class="hljs-keyword">null</span>);
            <span class="hljs-comment">//5. &#x51C6;&#x5907;&#x6D88;&#x606F;&#x5185;&#x5BB9;</span>
            String mes = <span class="hljs-string">&quot;&#x6768;&#x946B;&quot;</span>;
            <span class="hljs-comment">//6. &#x53D1;&#x9001;&#x6D88;&#x606F;&#x7ED9;&#x961F;&#x5217;queue</span>
            <span class="hljs-comment">/*
            * @params1 &#x4EA4;&#x6362;&#x673A;
            * @params2 &#x961F;&#x5217;&#x540D;&#x79F0;
            * @params3 &#x6D88;&#x606F;&#x7684;&#x72B6;&#x6001;&#x63A7;&#x5236;
            * @params4 &#x6D88;&#x606F;&#x4E3B;&#x9898;
            */</span>
            channel.basicPublish(<span class="hljs-string">&quot;&quot;</span>, queueName, <span class="hljs-keyword">null</span>, mes.getBytes());
            System.out.println(<span class="hljs-string">&quot;&#x6D88;&#x606F;&#x53D1;&#x9001;&#x6210;&#x529F;!!!!&quot;</span>);
        } <span class="hljs-keyword">catch</span> (Exception e) {
            e.printStackTrace();
        } <span class="hljs-keyword">finally</span> {
            <span class="hljs-comment">//7. &#x5173;&#x95ED;&#x901A;&#x9053;</span>
            <span class="hljs-keyword">if</span> (channel != <span class="hljs-keyword">null</span> &amp;&amp; channel.isOpen()) {
                <span class="hljs-keyword">try</span> {
                    channel.close();
                } <span class="hljs-keyword">catch</span> (Exception e) {
                    e.printStackTrace();
                }
            }
            <span class="hljs-comment">//8. &#x5173;&#x95ED;&#x8FDE;&#x63A5;</span>
            <span class="hljs-keyword">if</span> (connection != <span class="hljs-keyword">null</span> &amp;&amp; connection.isOpen()) {
                <span class="hljs-keyword">try</span> {
                    connection.close();
                } <span class="hljs-keyword">catch</span> (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
</code></pre>
<blockquote>
<p>consumer</p>
</blockquote>
<pre><code class="lang-java"><span class="hljs-keyword">package</span> com.yang.rabbitmq.simple;

<span class="hljs-keyword">import</span> com.rabbitmq.client.*;

<span class="hljs-keyword">import</span> java.io.IOException;


<span class="hljs-comment">/**
 * ClassName: Producer
 * Description:
 * Date: 2021/4/7 20:55
 *
 * <span class="hljs-doctag">@author</span> YangXin
 */</span>
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Consumer</span> </span>{

    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span> </span>{
        <span class="hljs-comment">//&#x6240;&#x6709;&#x7684;&#x4E2D;&#x95F4;&#x4EF6;&#x6280;&#x672F;&#x90FD;&#x662F;&#x57FA;&#x4E8E;tcp/ip&#x534F;&#x8BAE;&#x57FA;&#x7840;&#x4E0A;&#x6784;&#x5EFA;&#x65B0;&#x578B;&#x7684;&#x534F;&#x8BAE;&#x89C4;&#x8303;,&#x53EA;&#x4E0D;&#x8FC7;rabbitmq&#x9075;&#x5FAA;&#x7684;&#x662F;amqp&#x534F;&#x8BAE;</span>
        <span class="hljs-comment">//1. &#x521B;&#x5EFA;&#x8FDE;&#x63A5;&#x5DE5;&#x7A0B;</span>
        ConnectionFactory connectionFactory = <span class="hljs-keyword">new</span> ConnectionFactory();
        connectionFactory.setHost(<span class="hljs-string">&quot;39.105.46.187&quot;</span>);
        connectionFactory.setPort(<span class="hljs-number">5672</span>);
        connectionFactory.setUsername(<span class="hljs-string">&quot;admin&quot;</span>);
        connectionFactory.setPassword(<span class="hljs-string">&quot;admin&quot;</span>);
        connectionFactory.setVirtualHost(<span class="hljs-string">&quot;/&quot;</span>);

        Connection connection = <span class="hljs-keyword">null</span>;
        Channel channel = <span class="hljs-keyword">null</span>;
        <span class="hljs-keyword">try</span> {
            <span class="hljs-comment">//2. &#x521B;&#x5EFA;&#x8FDE;&#x63A5;Connection</span>
            connection = connectionFactory.newConnection(<span class="hljs-string">&quot;&#x751F;&#x4EA7;&#x8005;&quot;</span>);
            <span class="hljs-comment">//3. &#x901A;&#x8FC7;&#x8FDE;&#x63A5;&#x83B7;&#x53D6;&#x901A;&#x9053;Channel</span>
            channel = connection.createChannel();

            <span class="hljs-comment">// &#x6D88;&#x8D39;&#x6D88;&#x606F;</span>
            channel.basicConsume(<span class="hljs-string">&quot;queue1&quot;</span>, <span class="hljs-keyword">true</span>, <span class="hljs-keyword">new</span> DeliverCallback() {
                <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">handle</span><span class="hljs-params">(String s, Delivery delivery)</span> <span class="hljs-keyword">throws</span> IOException </span>{
                    System.out.println(<span class="hljs-string">&quot;&#x6536;&#x5230;&#x6D88;&#x606F;&#x662F; :&quot;</span> + <span class="hljs-keyword">new</span> String(delivery.getBody(), <span class="hljs-string">&quot;UTF-8&quot;</span>));
                }
            }, <span class="hljs-keyword">new</span> CancelCallback() {
                <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">handle</span><span class="hljs-params">(String s)</span> <span class="hljs-keyword">throws</span> IOException </span>{
                    System.out.println(<span class="hljs-string">&quot;&#x63A5;&#x6536;&#x5931;&#x8D25;&#x4E86;...&quot;</span>);
                }
            });

        } <span class="hljs-keyword">catch</span> (Exception e) {
            e.printStackTrace();
        } <span class="hljs-keyword">finally</span> {
            <span class="hljs-comment">//7. &#x5173;&#x95ED;&#x901A;&#x9053;</span>
            <span class="hljs-keyword">if</span> (channel != <span class="hljs-keyword">null</span> &amp;&amp; channel.isOpen()) {
                <span class="hljs-keyword">try</span> {
                    channel.close();
                } <span class="hljs-keyword">catch</span> (Exception e) {
                    e.printStackTrace();
                }
            }
            <span class="hljs-comment">//8. &#x5173;&#x95ED;&#x8FDE;&#x63A5;</span>
            <span class="hljs-keyword">if</span> (connection != <span class="hljs-keyword">null</span> &amp;&amp; connection.isOpen()) {
                <span class="hljs-keyword">try</span> {
                    connection.close();
                } <span class="hljs-keyword">catch</span> (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
</code></pre>
<p>&#x4F7F;&#x7528;&#x4EE3;&#x7801;&#x58F0;&#x660E;&#x961F;&#x5217;,&#x4EA4;&#x6362;&#x673A;&#x5E76;&#x7ED1;&#x5B9A;</p>
<p><img src="./imgs/&#x5FAE;&#x4FE1;&#x622A;&#x56FE;_20210414195632.png" alt=""></p>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="rabbitMQ11核心组成部分.html" class="navigation navigation-prev " aria-label="Previous page: rabbitMQ11核心组成部分">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="rabbitMQ13work模式.html" class="navigation navigation-next " aria-label="Next page: rabbitMQ13work模式">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"rabbitMQ12案例","level":"1.13","depth":1,"next":{"title":"rabbitMQ13work模式","level":"1.14","depth":1,"path":"rabbitMQ13work模式.md","ref":"rabbitMQ13work模式.md","articles":[]},"previous":{"title":"rabbitMQ11核心组成部分","level":"1.12","depth":1,"path":"rabbitMQ11核心组成部分.md","ref":"rabbitMQ11核心组成部分.md","articles":[]},"dir":"ltr"},"config":{"plugins":[],"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"theme":"default","author":"<yangxin19970404@163.com>","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"RabbitMQ","language":"zh-hans","gitbook":"3.2.3","description":"RabbitMQ > 消息队列"},"file":{"path":"rabbitMQ12案例.md","mtime":"2021-04-14T11:57:30.901Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2021-04-17T07:48:53.921Z"},"basePath":".","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="gitbook/gitbook.js"></script>
    <script src="gitbook/theme.js"></script>
    
        
        <script src="gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

